﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ElectronicJournal.DB
{
    public class SqlDb
    {
        SqlConnection cnn;

        public SqlDb()
        {
            cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        }

        public DataTable Read(string sql)
        {
            try
            {
                cnn.Open();
                SqlDataAdapter adp = new SqlDataAdapter(sql, cnn);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                cnn.Close();
                return dt;
            }
            catch (Exception)
            {
                //log
                return null;
            }
        }

        public bool Query(string sql)
        {
            try
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                int nRows = cmd.ExecuteNonQuery();
                cnn.Close();
                return nRows > 0;
            }
            catch (Exception)
            {
                //log
                return false;
            }
            
        }
    }
}